package org.example.controller;

import org.example.bean.R;
import org.example.dto.UserPointUsageDTO;
import org.example.entity.PointRecords;
import org.example.entity.Users;
import org.example.mapper.PointRecordsMapper;
import org.example.mapper.UsersMapper;
import org.example.service.UserPointUsageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class UserPointUsageController {
    
    @Autowired
    private UserPointUsageService userPointUsageService;
    
    @Autowired
    private UsersMapper usersMapper;
    
    @Autowired
    private PointRecordsMapper pointRecordsMapper;
    
    /**
     * 根据openid查询用户积分使用情况
     * @param openid 微信openid
     * @return 用户积分使用情况
     */
    @GetMapping("/user/point-usage")
    public R getUserPointUsage(@RequestParam String openid) {
        try {
            UserPointUsageDTO usage = userPointUsageService.getUserPointUsageByOpenid(openid);
            if (usage == null) {
                return new R().setCode(404).setMsg("用户不存在");
            }
            return new R().setMsg("查询成功").add("data", usage);
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据用户ID查询用户积分使用情况
     * @param userId 用户ID
     * @return 用户积分使用情况
     */
    @GetMapping("/user/point-usage/{userId}")
    public R getUserPointUsageByUserId(@PathVariable Long userId) {
        try {
            UserPointUsageDTO usage = userPointUsageService.getUserPointUsageByUserId(userId);
            if (usage == null) {
                return new R().setCode(404).setMsg("用户不存在");
            }
            return new R().setMsg("查询成功").add("data", usage);
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 调试接口：查看数据库中的积分记录
     */
    @GetMapping("/debug/point-records")
    public R debugPointRecords() {
        try {
            List<PointRecords> allRecords = pointRecordsMapper.selectList(null);
            return new R().setMsg("调试成功").add("totalRecords", allRecords.size()).add("records", allRecords);
        } catch (Exception e) {
            return new R().setCode(500).setMsg("调试失败: " + e.getMessage());
        }
    }
    
    /**
     * 调试接口：查看特定用户的积分记录
     */
    @GetMapping("/debug/user-point-records")
    public R debugUserPointRecords(@RequestParam String openid) {
        try {
            Users user = usersMapper.getUserByOpenid(openid);
            if (user == null) {
                return new R().setCode(404).setMsg("用户不存在");
            }
            
            List<PointRecords> userRecords = pointRecordsMapper.getPointRecordsByUserId(user.getId());
            
            return new R().setMsg("调试成功")
                    .add("userId", user.getId())
                    .add("userRecords", userRecords)
                    .add("recordCount", userRecords.size());
        } catch (Exception e) {
            return new R().setCode(500).setMsg("调试失败: " + e.getMessage());
        }
    }
} 